/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.enfymed.vista;

import basedatos.OperacionesBD;
import java.util.ArrayList;
import javax.sound.midi.Soundbank;
import javax.swing.JInternalFrame;
import modulos.enfymed.modelo.MedicamentoComercial;
import modulos.enfymed.modelo.MedicamentoGenerico;
import modulos.usuario.modelo.Usuario;
import vista.frmPrincipal;
import vista.modelo.Mensaje;
import vista.modelo.OperacionesVarias;

/**
 *
 * @author victor
 */
public class EnfyMed3 extends javax.swing.JInternalFrame {

    MedicamentoComercial medicCom;
    JInternalFrame parent;

    public EnfyMed3(JInternalFrame parent) {
        initComponents();
        this.parent = parent;
        OperacionesVarias.centrar(this);
        btnEditar.setVisible(false);
        btnGuardar.setLocation(230, 190);
        ponerCombo();
    }

    public EnfyMed3(JInternalFrame parent, MedicamentoComercial mc) {
        initComponents();
        this.parent = parent;
        txtNombreComercial.setText(mc.getNombreComercial());
        txtDosis.setText(mc.getDosis());
        txtPresentacion.setText(mc.getPresentacion());
        OperacionesVarias.centrar(this);
        btnGuardar.setVisible(false);
        btnEditar.setLocation(230, 190);
        this.medicCom = mc;
        cbGenericos.addItem(medicCom.getMedicamentoGenerico().getNombreGenerico());
        cbGenericos.setEditable(false);

    }

    private void ponerCombo() {
        ArrayList<Object> medics = EnfyMed1.medicamentosGenericos;
        if (!medics.isEmpty()) {
            for (Object o : medics) {
                MedicamentoGenerico mg = (MedicamentoGenerico) o;
                cbGenericos.addItem(mg.getNombreGenerico());
            }
        } else {
            System.out.println("Combo vacio");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtNombreComercial = new javax.swing.JTextField();
        txtPresentacion = new javax.swing.JTextField();
        txtDosis = new javax.swing.JTextField();
        btnGuardar = new javax.swing.JButton();
        btnCerrar = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        cbGenericos = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();

        setClosable(true);
        setTitle("MEDICAMENTOS");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/Pills.png"))); // NOI18N
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        jLabel1.setText("Comercial:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(40, 60, 120, 30);

        jLabel2.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        jLabel2.setText(" Generico:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(40, 20, 110, 30);

        jLabel3.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        jLabel3.setText(" Presentacion:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(10, 100, 140, 30);

        jLabel4.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        jLabel4.setText("  Dosis:");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(60, 140, 80, 30);
        getContentPane().add(txtNombreComercial);
        txtNombreComercial.setBounds(120, 60, 273, 30);
        getContentPane().add(txtPresentacion);
        txtPresentacion.setBounds(120, 100, 273, 30);
        getContentPane().add(txtDosis);
        txtDosis.setBounds(120, 140, 273, 30);

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/filesave.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });
        getContentPane().add(btnGuardar);
        btnGuardar.setBounds(280, 190, 110, 30);

        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        btnCerrar.setText("Cerrar");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCerrar);
        btnCerrar.setBounds(410, 190, 100, 30);

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/icono_editar.png"))); // NOI18N
        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });
        getContentPane().add(btnEditar);
        btnEditar.setBounds(150, 190, 110, 30);
        getContentPane().add(cbGenericos);
        cbGenericos.setBounds(120, 20, 270, 30);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/add.png"))); // NOI18N
        jButton1.setText("Crear");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(410, 20, 100, 30);

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/delete.png"))); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        getContentPane().add(btnEliminar);
        btnEliminar.setBounds(410, 70, 100, 30);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-543)/2, (screenSize.height-286)/2, 543, 286);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        dispose();
        parent.show();
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        if (!verificaVacios()) {
            medicCom = new MedicamentoComercial();
            medicCom.setNombreComercial(txtNombreComercial.getText());
            medicCom.setDosis(txtDosis.getText());
            medicCom.setPresentacion(txtPresentacion.getText());
            MedicamentoGenerico mg = devuelveGenerico();
            medicCom.setMedicamentoGenerico(mg);
            Usuario u=(Usuario) frmPrincipal.UsuarioLogueado.get(0)[1];
            medicCom.setUsuario(u);
            if (mg != null&&u!=null) {
                if (OperacionesBD.guardar(medicCom)) {
                    Mensaje.datosGuardados();
                    EnfyMed1.listar();
                    EnfyMed1.poneTabla(EnfyMed1.medicamentosComerciales);
                    poneVacios();
                } else {
                    Mensaje.datosNoGuardados();
                }
            }

        } else {
            Mensaje.camposVacios();
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        if (!verificaVacios()) {
            medicCom.setNombreComercial(txtNombreComercial.getText());
            medicCom.setDosis(txtDosis.getText());
            medicCom.setPresentacion(txtPresentacion.getText());
            if (OperacionesBD.modificar(medicCom)) {
                Mensaje.datosModificados();
                EnfyMed1.listar();
                EnfyMed1.poneTabla(EnfyMed1.medicamentosComerciales);
                dispose();
            } else {
                Mensaje.datosNoModificados();
            }

        } else {
            Mensaje.camposVacios();
        }
    }//GEN-LAST:event_btnEditarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        EnfyMed4 em4 = new EnfyMed4(this);
        frmPrincipal.dskpPrincipal.add(em4);
        em4.setVisible(true);
        this.hide();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        MedicamentoGenerico mg = devuelveGenerico();
        if (mg != null) {
            if (OperacionesBD.eliminar(mg)) {
                Mensaje.datosEliminados();
                cbGenericos.removeItemAt(index);
            } else {
                Mensaje.datosNoEliminados();
            }
        }
    }//GEN-LAST:event_btnEliminarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGuardar;
    public static javax.swing.JComboBox cbGenericos;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtDosis;
    private javax.swing.JTextField txtNombreComercial;
    private javax.swing.JTextField txtPresentacion;
    // End of variables declaration//GEN-END:variables
    int index=0;
    private boolean verificaVacios() {
        if (txtNombreComercial.getText().isEmpty()
                || txtPresentacion.getText().isEmpty()
                || txtDosis.getText().isEmpty()) {
            return true;
        }
        return false;
    }

    private void poneVacios() {
        txtDosis.setText("");
        txtNombreComercial.setText("");
        txtPresentacion.setText("");
    }

    private MedicamentoGenerico devuelveGenerico() {
        MedicamentoGenerico mg = (MedicamentoGenerico) OperacionesBD.buscar(
                "MedicamentoGenerico", "nombreGenerico",
                String.valueOf(cbGenericos.getSelectedItem()));
        index=cbGenericos.getSelectedIndex();
        return mg;
    }
}
